﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.CustomerManager;
using PayrollHR.Web;
using Payroll.BusinessLogic.Utils;
using MigraDoc.DocumentObjectModel;

namespace PayrollHR.Administration.Modules
{
    public partial class CaseProfileSupport : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<ProfileProperty> customer = CustomerService.GetAllProfilePropertyByCustomerId(this.CustomerID);
                BindGrid(customer);
            }
        }
        protected void BindGrid(List<ProfileProperty> dt)
        {
            ViewState["CaseProfileSupport"] = dt;
            gvObject.DataSource = dt;
            gvObject.DataBind();
        }
        protected void gvObject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvObject.PageIndex = e.NewPageIndex;
            List<ProfileProperty> customer = CustomerService.GetAllProfilePropertyByCustomerId(this.CustomerID);
            BindGrid(customer);
        }
        private List<ProfileProperty> getTableGridview()
        {
            List<ProfileProperty> dtCurrentTable = new List<ProfileProperty>();
            if (ViewState["CaseProfileSupport"] != null)
            {
                dtCurrentTable = (List<ProfileProperty>)ViewState["CaseProfileSupport"];
            }
            return dtCurrentTable;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<ProfileProperty> listProfile = getTableGridview();
            ProfileProperty profile = new ProfileProperty()
            {
                PaperRequest = txtPaperRequest.Text,
                OnRequest = dpOnRequest.SelectText,
                DateSubmitted = dpDateSubmitted.SelectText,
                ProTo = txtProTo.Text.Trim(),
                Postage = txtPostage.Value,
                ProComplete = txtProComplete.Text.Trim(),
                Note = txtNote.Text.Trim(),
                UpdateByID = 1,
                CreateByID = 1,
                fromdate = dpFromdate.SelectText,
                todate = dpTodate.SelectText,
                CreatedOn = Convert.ToDateTime(DateTime.Today),
                UpdatedOn = Convert.ToDateTime(DateTime.Today)
            };
            listProfile.Add(profile);
            BindGrid(listProfile);
        }

        protected void gvObject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btnDelete = e.Row.FindControl("lbDelete") as LinkButton;
                if (btnDelete != null)
                    btnDelete.CommandArgument = e.Row.RowIndex.ToString();
            }
        }
        public List<ProfileProperty> GetProfileProperty()
        {
            if (ViewState["CaseProfileSupport"] != null)
            {
                return (List<ProfileProperty>)ViewState["CaseProfileSupport"];
            }
            else
            {
                return new List<ProfileProperty>();
            }
        }
        private void Delete()
        {
            List<ProfileProperty> listProfile = getTableGridview();
            List<int> List = new List<int>();
            foreach (GridViewRow row in gvObject.Rows)
            {
                CheckBox chkCheck = row.FindControl("chkCheck") as CheckBox;
                HiddenField hfProfileID = row.FindControl("hfProfileID") as HiddenField;
                if (chkCheck.Checked)
                {
                    //listProfile.RemoveAt(row.RowIndex);
                    gvObject.DeleteRow(row.RowIndex);
                    if (CommonHelper.ToInteger(hfProfileID.Value) > 0)
                    {
                        List.Add(CommonHelper.ToInteger(hfProfileID.Value));
                    }
                }

            }
            if (List.Count > 0)
            {
                this.CustomerService.DeleteProfileProperty(List);
            }
            //BindGrid(listProfile);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                this.Delete();
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void gvObject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}